package com.gmscosmo.janus.controller;

import com.alibaba.fastjson.JSONObject;
import com.gmscosmo.common.APIPath;
import com.gmscosmo.common.IMResult;
import com.gmscosmo.common.JanusIMSignaling;
import com.gmscosmo.janus.util.JanusLogUtil;
import com.gmscosmo.common.JanusNativeSignaling;
import com.gmscosmo.janus.handler.JanusIMProtocolProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class JanusController extends BaseController{
    @Autowired
    JanusLogUtil janusLogUtil;
    @Autowired
    JanusIMProtocolProcessor protocolProcessor;

    @PostMapping(APIPath.ToJanus)
    public IMResult<String> handleJanus(@RequestBody  JSONObject jsonObject) {
        String response = "";
        IMResult result = null;
        try {
            janusLogUtil.log("传入参数"+ jsonObject,this.getClass(),JanusLogUtil.TRACE);
            String transaction = jsonObject.getString("transaction");
            String fromUser = jsonObject.getString("fromUser");
            switch (jsonObject.getString("janus")) {
                case JanusNativeSignaling.MESSAGE:
                    response = protocolProcessor.handleMessage(jsonObject, fromUser);
                    break;
                case JanusIMSignaling.UN_PUBLISH:
                    System.out.println("UN_PUBLISH" + jsonObject.toString());
                    break;
                case JanusIMSignaling.CREATE_ROOM:
                    response = protocolProcessor.handleCreateRoom(jsonObject, fromUser);
                    break;
                case JanusIMSignaling.JOIN_PUB:
                    response = protocolProcessor.handleJoinAndPub(jsonObject, fromUser);
                    break;
                case JanusNativeSignaling.KEEP_A_LIVE:
                    response = protocolProcessor.handleKeepALive(jsonObject, fromUser);
                    break;
                case JanusNativeSignaling.LEAVE:
                    response = protocolProcessor.handleLeave(jsonObject, fromUser);
                    break;
                case JanusNativeSignaling.TRICKLE:
                    response = protocolProcessor.handleCandidate(jsonObject, fromUser);
                    break;
                case JanusIMSignaling.RESTART_ICE:
                    response = protocolProcessor.handleRestartICE(jsonObject, fromUser);
                    System.out.println("RESTART_ICE" + jsonObject.toString());
                    break;
                case JanusIMSignaling.JOIN_SUB:
                    response = protocolProcessor.handleJoinAndSub(jsonObject, fromUser);
                    break;
                case JanusNativeSignaling.CONFIGURE:
                    response = protocolProcessor.handleConfigure(jsonObject, fromUser);
                    break;
                default:
                    jsonObject = new JSONObject();
                    jsonObject.put("msg", "未知信令");
                    response = jsonObject.toJSONString();
            }
            jsonObject = JSONObject.parseObject(response);
            jsonObject.put("transaction", transaction);
            response = jsonObject.toJSONString();
            result = new IMResult();
            result.setResult(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
